import json

from django.http import HttpResponse
from django.views.generic.base import View

from common.const import MESSAGE_CODE_MAP


class BaseView(View):

    def response_json(self, code, data=None, message=""):
        return response_json(code, data, message)


def response_json(code, data=None, message=""):
    if not data:
        data = {}
    return HttpResponse(json.dumps({
        "code": code,
        "data": data,
        "message": message if message != "" else MESSAGE_CODE_MAP.get(code)
    }), content_type='application/json')
